#include "Heap.h"

bool Heap::isEmpty() 
{ 
	return _heapSize==0; 
}
int Heap::getSize() 
{ 
	return _heapSize; 
}

int Heap::right(int i) 
{ 
	if (isEmpty()) 
		return -1;
	return 2*i+2;
}

int Heap::left(int i)
{
	if (isEmpty())
		return -1;
	return 2*i+1;
}

int Heap::parent(int i)
{
	if (isEmpty())
		return -1;
	return (i-1)/2;
}

int Heap::getFirst()
{
	return _arr[0]->num;
}
int Heap::extract(int index)
{
	if (isEmpty())
		return -1;
	_heapSize--;
	HeapLink* extracted=_arr[index];
	_arr.erase(_arr.begin() + index);
	if(index==_heapSize)
	{
		int ans = extracted->num;
		delete extracted;
		return ans;
	}

	HeapLink* tmp = _arr[_heapSize-1];
	_arr.erase(_arr.begin() + _heapSize-1);
	posIT(tmp,index);
	_arr.insert(_arr.begin()+index,tmp);
	heapify(index);
	int ans = extracted->num;
	delete extracted;
	return ans;
}